package com.dz.xjd.utils;

import org.bouncycastle.util.encoders.Base64;

/**
 * Created by dz on 2017/7/25.
 */

public class EncryptDecrypt {

    private static byte[] decryptBASE64(byte[] key) throws Exception {
        return (new Base64()).decode(key);
    }
    private static byte[] encryptBASE64(byte[] key) throws Exception {
        return (new Base64()).encode(key);
    }


    public static String  decrypt(String sessionKey,String  encryptedData ,String iv) throws Exception{
        byte[] sessionKeyBy = sessionKey.getBytes();
        byte[] encryptedDataBy = decryptBASE64(encryptedData.getBytes());
        byte[] ivBy = iv.getBytes();
        byte[] dec = Pkcs7Encoder.decryptOfDiyIV(encryptedDataBy, sessionKeyBy,ivBy);
        return new String(dec);
    }

    public static String  encrypt(String sessionKey,String  encryptedData ,String iv) throws Exception{
        byte[] sessionKeyBy = sessionKey.getBytes();
        byte[] encryptedDataBy = encryptedData.getBytes();
        byte[] ivBy = iv.getBytes();
        byte[] dec = Pkcs7Encoder.encryptOfDiyIV(encryptedDataBy, sessionKeyBy,ivBy);
        byte[] DataBy = encryptBASE64(dec);
        return new String(DataBy);
    }
}
